$(function() {
	//加载订单消费
	$.ajax({
        url:"/a/wxconf/point",
        type: "GET",
        async: false,// 这里应设置为同步的方式
        dataType : 'json',
        contentType : "application/json; charset=utf-8",
        success: function (data) {
        	if(data.status=='1'){
        		if(data.data){
        			if(data.data.data){
        			var datas = data.data.data;
        			for(var i = 0;i<datas.length;i++){
        				var str = '<tr class="tr_' + i + '" id='+datas[i].id+'>'
        				+'<td><label class="form-control-static pull-left margin-left">消费金额</label>'
        				+'<div class="col-xs-2"><input type="text" class="form-control" name="value1" value='+datas[i].value1/100+' maxlength = "5" oninput="if(value.length>5)value=value.slice(0,5)"/></div>'
        				+'<label class="form-control-static pull-left margin-left">至</label>'
        				+'<div class="col-xs-2"><input type="text" class="form-control" name="value2" value='+datas[i].value2/100+' maxlength = "5" oninput="if(value.length>5)value=value.slice(0,5)"/></div>'
        				+'<label class="form-control-static pull-left margin-left">元</label><label class="form-control-static pull-left margin-left">赠送积分</label>'
        				+'<div class="col-xs-4"><input type="number" class="form-control" name="givIntegral" placeholder="数字,不含小数点8位数" value='+datas[i].givIntegral+' maxlength = "5" oninput="if(value.length>5)value=value.slice(0,5)"/></div>'
        				+'<button type="button" class="btn red btn_del_role">删除</button></td></tr>';
        				$("#table_spec tbody:last").append(str);
        			}
        		}
        			if(data.data.inteRule){
            			var inteRule = data.data.inteRule;
            			 $("input[name='id']").val(inteRule.id);
                		$("input[name='bindPhone']").val(inteRule.bindPhone);
                		$("input[name='perfectInfo']").val(inteRule.perfectInfo);
                		$("input[name='sign']").val(inteRule.sign);
            		}
        		}
        		}else{
        			$('#modal_new_role .help-block').html(data.message); // 返回错误信息
					$("#modal_new_role .help-block").attr("style","color:red;");
        	}
        }
    });
	
	     $("#table_spec tbody").on('click','.btn_cancle_role',function(){
	    	 $(this).parent().parent().remove();
	     });
		 $("#table_spec tbody").on('click','.btn_del_role',function(){
			 
		$.ajax({
	        url:"/a/wxconf/point",
	        type: "delete",
	        async: false,// 这里应设置为同步的方式
	        dataType : 'json',
	        data:JSON.stringify({id:$(this).parent().parent().attr("id")}),
	        contentType : "application/json; charset=utf-8",
	        success: function (data) {
	        	if(data.status=='1'){
	        		window.location.reload();
	        		}
	        	}
		})
		
	})
	
	$('#saleAmount_add').click(function(event){
		getCount = $("#table_spec tbody").find("tr").length;
		    if(getCount ==10){
		    	alert("消费送积分最多只能添加10条记录");
		    	return;
		    }
	 	   addRow();
		 });
	
	$("#save").click(function() {
		var _data = $("#modal_new_role").serializeObject();
		if (!mdTool.validate(_data.bindPhone, 'require')) {
			mdTool.showMessage('请输入绑定手机号赠送积分');
			return;
		}
		if (!mdTool.validate(_data.bindPhone, 'num')) {
			mdTool.showMessage('请输入正整数绑定手机号赠送积分');
			return;
		}
		if (!mdTool.validate(_data.perfectInfo, 'require')) {
			mdTool.showMessage('请输入完善个人资料赠送积分');
			return;
		}
		if (!mdTool.validate(_data.perfectInfo, 'num')) {
			mdTool.showMessage('请输入正整数完善个人资料赠送积分');
			return;
		}
		if (!mdTool.validate(_data.sign, 'require')) {
			mdTool.showMessage('请输入每日签到赠送积分');
			return;
		}
		if (!mdTool.validate(_data.sign, 'num')) {
			mdTool.showMessage('请输入正整数每日签到赠送积分');
			return;
		}
		var ddxfs = _data.value1;
		if(!ddxfs){
			mdTool.showMessage('请输入消费送积分');
			return;
		}
		var array="";
		var saleR = "";
		if(ddxfs instanceof Array){
			var arrLen = ddxfs.length;
			 array=new Array(arrLen);
			   for(var i=0;i<arrLen;i++){
	    	    	if (!mdTool.validate(_data.value1[i], 'require')) {
	    	    		mdTool.showMessage('请输入消费金额范围');
	    	    		return;
	    	    	}
	    	    	if (!mdTool.validate(_data.value1[i], 'znum')) {
	    	    		mdTool.showMessage('请输入正数消费金额范围');
	    	    		return;
	    	    	}
	    	    	if (!mdTool.validate(_data.value2[i], 'require')) {
	    	    		mdTool.showMessage('请输入消费金额范围');
	    	    		return;
	    	    	}
	    	    	if (!mdTool.validate(_data.value2[i], 'znum')) {
	    	    		mdTool.showMessage('请输入正数消费金额范围');
	    	    		return;
	    	    	}
	    	    	if (!mdTool.validate(_data.givIntegral[i], 'require')) {
	    	    		mdTool.showMessage('请输入赠送积分');
	    	    		return;
	    	    	}
	    	    	if (!mdTool.validate(_data.givIntegral[i], 'num')) {
	    	    		mdTool.showMessage('请输入正整数赠送积分');
	    	    		return;
	    	    	}
	    	    	
	    	        var srId = $("#table_spec tbody").find("tr").eq(i).attr("id");
		    	    var saleRules = {"id":srId,"value1":_data.value1[i],"value2":_data.value2[i],"givIntegral":_data.givIntegral[i]};
		    	    array[i] = saleRules;
			   }
			   
		}else{
			
			if (!mdTool.validate(_data.value1, 'require')) {
	    		mdTool.showMessage('请输入消费金额范围');
	    		return;
	    	}
			if (!mdTool.validate(_data.value1, 'znum')) {
	    		mdTool.showMessage('请输入正数消费金额范围');
	    		return;
	    	}
	    	if (!mdTool.validate(_data.value2, 'require')) {
	    		mdTool.showMessage('请输入消费金额范围');
	    		return;
	    	}
	    	if (!mdTool.validate(_data.value2, 'znum')) {
	    		mdTool.showMessage('请输入正数消费金额范围');
	    		return;
	    	}
	    	if (!mdTool.validate(_data.givIntegral, 'require')) {
	    		mdTool.showMessage('请输入赠送积分');
	    		return;
	    	}
	    	if (!mdTool.validate(_data.givIntegral, 'num')) {
	    		mdTool.showMessage('请输入正整数赠送积分');
	    		return;
	    	}
			
			  var srId = $("#table_spec tbody").find("tr").eq(0).attr("id");
			 var saleRules = {"id":srId,"value1":_data.value1,"value2":_data.value2,"givIntegral":_data.givIntegral};
			 saleR = saleRules;
		}
		var jfgz={"id":_data.id,"bindPhone":_data.bindPhone,"perfectInfo":_data.perfectInfo,"sign":_data.sign};
		var params = {"IntegralRule":jfgz,"SaleRule":saleR,"SaleRules":array};
		var urls = "/a/wxconf/point";
		var types= "put";
		if (_data.id != null && _data.id != '') {
			types = "post";
		}
		$.ajax({
            url:urls,
            type: types,
            async: false,// 这里应设置为同步的方式
            data: JSON.stringify(params),
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            success: function (data) {
            	if(data.status=='1'){
            		// 跳转到列表页面
            		//window.location.href = "/p/wxconf/group";
            		 $("input[name='id']").val(data.data.id);
            		$('#modal_new_role .help-block').html("保存成功!"); // 返回错误信息
					$("#modal_new_role .help-block").attr("style","color:green;");
					setTimeout(function(){$('#modal_new_role .help-block').html("");},2000);
					window.location.reload();
            		}else{
            			$('#modal_new_role .help-block').html(data.message); // 返回错误信息
						$("#modal_new_role .help-block").attr("style","color:red;");
            	}
            }
        });
	})	



var i = 0;
function addRow(){
	if(i>9){
		alert("消费送积分最多只能添加10条记录");
		return;
	}
	var str = '<tr class="tr_' + i + '">'
	+'<td><label class="form-control-static pull-left margin-left">消费金额</label>'
	+'<div class="col-xs-2"><input type="text" class="form-control" name="value1" maxlength = "5" /></div>'
	+'<label class="form-control-static pull-left margin-left">至</label>'
	+'<div class="col-xs-2"><input type="text" class="form-control" name="value2" maxlength = "5" /></div>'
	+'<label class="form-control-static pull-left margin-left">元</label><label class="form-control-static pull-left margin-left">赠送积分</label>'
	+'<div class="col-xs-4"><input type="number" class="form-control"  name="givIntegral"   placeholder="请输入赠送积分"  oninput="if(value.length>5)value=value.slice(0,5)" maxlength = "5" /></div>'
	+'<button type="button" class="btn green btn_cancle_role">取消</button></td></tr>';
	$("#table_spec tbody:last").append(str);
	    i++
}

})